package com.hy.common.utils;

import com.alibaba.excel.EasyExcel;
import com.hy.common.excel.AbstractExcelImport;
import com.hy.common.exception.ImportException;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;


@Slf4j
public class ExcelUtil {
    public static String Bytes2HexString(Integer decimal) {
        String hexadecimal = Integer.toHexString(decimal).toUpperCase(); // 将10进制数字转换为16进制字符串
        int length = hexadecimal.length(); // 获取16进制字符串的长度
        if (length < 24) {
            // 如果长度小于24，则在前面补0
            for (int i = 0; i < 24 - length; i++) {
                hexadecimal =  hexadecimal+ "0";
            }
        } else if (length > 24) {
            // 如果长度大于24，则截取前24个字符
            hexadecimal = hexadecimal.substring(0, 24);
        }
      return hexadecimal;


    }

    /**
     * 从Excel中指定sheet读取文件
     */
    public static <T,E> void readExcel(final InputStream inputStream, Class<?> clazz,
                                     AbstractExcelImport<T,E> listener, Integer sheetNo) throws RuntimeException {

        try{
            if(sheetNo != null){
                EasyExcel.read(inputStream, clazz, listener)
//                    .registerConverter(new TimestampStringConverter())
//                    .registerConverter(new BigDecimalConverter())
                        .sheet(sheetNo).doRead();
            }
        }
        catch (Exception e){
            e.printStackTrace();
            throw new ImportException(e.getMessage());
        }
    }

    /**
     * 从Excel中读取文件(第一个sheet)
     */
    public static <T,E> void readExcel(final InputStream inputStream, Class<?> clazz, AbstractExcelImport<T,E> listener) {
        EasyExcel.read(inputStream, clazz, listener)
//                .registerConverter(new TimestampStringConverter())
//                .registerConverter(new BigDecimalConverter())
                .sheet().doRead();
    }

    /**
     * 写入到指定excel文件
     */
    public static <T> void write(String fileName, String sheetName, Class<T> head, List<T> datas){
        EasyExcel.write(fileName).head(head).sheet(sheetName).doWrite(datas);
    }

    /**
     * 写入到指定excel的sheet文件
     */
    public static <T> void write(OutputStream outputStream, String sheetName, Class<T> head, List<T> data){
        EasyExcel.write(outputStream).head(head).sheet(sheetName).doWrite(data);
    }

    /**
     * 写入到指定excel的sheet文件
     */
    public static <T> void write(OutputStream outputStream, Integer sheetNo, String sheetName, Class<T> head, List<T> data){
        EasyExcel.write(outputStream).head(head).sheet(sheetNo).sheetName(sheetName).doWrite(data);
    }

    /**
     * 写入到指定excel文件
     */
    public static <T> void write(String fileName, String sheetName, List<List<String>> head, List<T> datas){
        EasyExcel.write(fileName).head(head).sheet(sheetName).doWrite(datas);
    }

    /**
     * 写入到指定excel的sheet文件
     */
    public static <T> void write(OutputStream outputStream, String sheetName, List<List<String>> head, List<T> data){
        EasyExcel.write(outputStream).head(head).sheet(sheetName).doWrite(data);
    }

    /**
     * 写入到指定excel的sheet文件
     */
    public static <T> void write(OutputStream outputStream, Integer sheetNo, String sheetName, List<List<String>> head,
                                 List<T> data){
        EasyExcel.write(outputStream).head(head).sheet(sheetNo).sheetName(sheetName).doWrite(data);
    }
}
